package RSA;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

public class OAEP_RSA_TEST {
    private static class RSA_OAEP_TESTData {
        private final BigInteger e;
        private final BigInteger n;
        private final String seed;
        private final BigInteger d;
        private final byte[] pt;
        private final byte[] EM;
        private final byte[] ct;

        private RSA_OAEP_TESTData(String e, String n, String seed, String d,
                                  String pt, String em, String ct) {
            this.e = new BigInteger(e);
            this.n = new BigInteger(n);
            this.seed = seed;
            this.d = new BigInteger(d);
            this.pt = pt.getBytes(StandardCharsets.UTF_8);
            try {
                EM = Hex.decodeHex(em);
                this.ct = Hex.decodeHex(ct);
            } catch (DecoderException ex) {
                throw new RuntimeException(ex);
            }
        }

        public BigInteger getE() {
            return e;
        }

        public BigInteger getN() {
            return n;
        }

        public String getSeed() {
            return seed;
        }

        public BigInteger getD() {
            return d;
        }

        public byte[] getPt() {

            return pt;
        }

        public byte[] getEM() {

            return EM;
        }

        public byte[] getCt() {

            return ct;
        }
    }

    public static void main(String[] args) throws DecoderException {
        primaryTest();
    }

    private static void primaryTest() {
        RSA_OAEP_TESTData[] datas = new RSA_OAEP_TESTData[]{
                new RSA_OAEP_TESTData("65537",
                        "8968589805326203310470947027342270154914493723822992572356534576855469981085784058571986094623043766925652886956312921217960693506095224925929819788129199",
                        "078f69d1571a0ec172bb85bddc05073b8060f9e5",
                        "1136930956599327053471971983813106801456118129568357146209592890497203787217283672995412641142415848546732916187273734131683301756103810874029951376740449",
                        "~~How beautiful it is",
                        "00364589d9a74908bc683895e581f3100c71bb0117da39a378d3805906c3d1852b9f908910bce2122ea337c60e32491fd709d5add434e6cea2336ec69b420409",
                        "7ca1cf396d7240b868d6b488d5d0e056ede5d4d83a516fb7f507d80192b3f75cc452581ad73b16c8abb5d797821a494a414fabed54cf212090dcc6c01dc5212e"),
                new RSA_OAEP_TESTData("65537",
                        "8607371926839837272779394520229641778202877100035455694317896887572699795305141674572213889395584064034353684537019257868757750135473115492382434270747661",
                        "a44babb31329f4d9e55a99ff9bee5fbaf0c07d55",
                        "237586948069842464965712874972846208657231864045716760776676922495979582979182182709069824639238083420416090978414831729001826412742899880910385800266657",
                        "I love Cryptography!",
                        "00ae8ca9300f050671e4236537845c1d9754c8c0ccda39a384cc7afe7f3dc2a9c63ab8fbc630ff33dbab6ec00b483016e6c20297c30955473ab23d781f444975",
                        "685672d06872d0a02d0ea79f70fde6133361abc0dae9e28ce6e6a6f5c0a7e3f7bdf72f2405a7fc72e7cd561eb07968504e221b014f20d6ca81e7314c34bf523f"),
                new RSA_OAEP_TESTData("65537",
                        "5834741948909160468390565803661602014573298818654991875119494762357609045350556851837230381333420788673736852763176990736667382100193882700713560469786133",
                        "6c48a93c4301a04c370223e64a28a738a4bbe747",
                        "2056675980922997939794169104938991228445882112543280395915825388791417467333561973310207293638830815669334269225484598841401378445132525238055912375054717",
                        "RSA is so weird...",
                        "000bbb93bb9ed7af67c0280c41682e9287c14be56dda39a318cf88c5f08001c66a1bcd173e4e93dbb7aae8300fec555679ac2a6a286924145f75d6bbe7393754",
                        "5f3cd82d4a9711179b9a6c23bec7e097204a54c6edc387d65e5e82d6fcefe20bc766a7976b4f517d76e42768dfcbe3ce89461791131acf6912173a30fc5d3270")
        };
        boolean flag = true;
        for (RSA_OAEP_TESTData d : datas) {
//            // 用以测试这些测试数据的正确性
//            RSA rsa = new RSA();
//            BigInteger encrypt = rsa.encrypt(BigInteger.valueOf(12345L), d.getE(), d.getN());
//            BigInteger decrypt = rsa.decrypt(encrypt, d.getD(), d.getN());
//            System.out.println(decrypt);

            OAEP_RSA rsaOaep = null;
            try {
                rsaOaep = new OAEP_RSA(Hex.decodeHex(d.getSeed()));
            } catch (DecoderException e) {
                throw new RuntimeException(e);
            }
            BigInteger encryptOAEP = rsaOaep.encrypt(d.getPt(), "666", 64, d.getE(), d.getN());
            byte[] decryptOAEP = rsaOaep.decrypt(encryptOAEP, "666", 64, d.getD(), d.getN());
            System.out.println(new String(decryptOAEP));
        }

    }

}
